package com.atsmart.app.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.Dialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.util.Log;

import com.atsmart.app.BizApp;
import com.atsmart.app.R;
import com.atsmart.app.model.ClientInfo;
import com.atsmart.app.model.ClientListInfo;
import com.atsmart.app.model.MapMarker;

import com.atsmart.app.utils.DialogUtil;
import com.atsmart.app.utils.PersistentKeyUtil;
import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.MapActivity;
import com.baidu.mapapi.MapController;
import com.baidu.mapapi.MapView;
import com.baidu.mapapi.MyLocationOverlay;
import com.baidu.mapapi.OverlayItem;
import com.baidu.mapapi.RouteOverlay;

public class ClientsMapActivity extends MapActivity {
	private BMapManager mapManager;
	private MapView mapView;
	private ClientListInfo clients;
	/** 用MapController完成地图控制  */
	private MapMarker marker = null;
	private GeoPoint point = null;
	private int center_latitu;
	private int center_long ;
	private int span_latitu;
	private int span_long;
	private MapController mMapController = null;
	private Dialog dialog;
	private Context context;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.client_map_layout);
		clients = (ClientListInfo) getIntent().getExtras().getSerializable(
				PersistentKeyUtil.CLIENTS_STRING);
		if (clients == null) {
			this.finish();
			return;
		}
		context = this;
		dialog = DialogUtil.getLoadingDialog(context);
		mapManager = new BMapManager(getApplication());
		mapManager.init("53BD9B67787BE8DBF3B721511595184902D8B50C", null);
		mapManager.getLocationManager();
		super.initMapActivity(mapManager);
		mapView = (MapView) findViewById(R.id.map_client);
		mapView.setBuiltInZoomControls(true);
		marker = new MapMarker(this, clients);
		point = new GeoPoint((int) (BizApp.getInstance().getLat() * 1e6),
				(int) (BizApp.getInstance().getLon() * 1e6));
		mapView.getOverlays().clear();
		MyLocationOverlay myLoc = new MyLocationOverlay(this, mapView);
		myLoc.enableMyLocation();
		mapView.getOverlays().add(myLoc);
		mapView.getOverlays().add(marker);
		mMapController = mapView.getController();
		//更具gepoint控制地图缩放级别
//		if (clients != null && clients.getClients().size() > 1) {
//			try{
//				new AsyncTask<Void, Void, Void>() {
//
//					@Override
//					protected Void doInBackground(Void... params) {
//						Message msg = new Message();
//						msg.what = 0x001;
//						handler.sendMessage(msg);
//						GeoPoint gp = new GeoPoint((int) (clients.getClients().get(0).getLat() * 1E6), (int) (clients.getClients().get(0).getLon() * 1E6));
//						int max_latitu = gp.getLatitudeE6();
//						int min_latitu = gp.getLatitudeE6();
//						int max_long = gp.getLongitudeE6();
//						int min_long = gp.getLongitudeE6();
//						
//			            for(ClientInfo client: clients.getClients()){
//			            	gp = new GeoPoint((int) (client.getLat() * 1E6), (int) (client.getLon() * 1E6));
//							if (max_latitu < gp.getLatitudeE6()) {
//								max_latitu = gp.getLatitudeE6();
//							}
//							if (min_latitu > gp.getLatitudeE6()) {
//								min_latitu = gp.getLatitudeE6();
//							}
//							if (max_long < gp.getLongitudeE6()) {
//								max_long = gp.getLongitudeE6();
//							}
//							if (min_long > gp.getLongitudeE6()) {
//								min_long = gp.getLongitudeE6();
//							}
//			            }
//						// 计算中心点
//						center_latitu = (max_latitu + min_latitu) / 2;
//						center_long = (max_long + min_long) / 2;
//						// 计算缩放距离
//						span_latitu = max_latitu - min_latitu;
//						span_long = max_long - min_long;
//						return null;
//					}
//					protected void onPostExecute(Void result) {
						
//				        if(span_latitu > 0 && span_long > 0){
//				        	try{
//								SystemClock.sleep(1000);
//								mMapController.zoomToSpan(span_latitu, span_long);// 只有在地图加载完成后该方法才起作用，所有休眠1s
//								mapView.invalidate();
//								GeoPoint po = new GeoPoint(center_latitu,center_long);
//								mMapController.setCenter(po);
//				        	}catch (Exception e) {
//				        		mMapController.setCenter(point);
//					        	mMapController.animateTo(point);
//					        	mMapController.setZoom(10);
//				        	}
//				        }else{
//				        	mMapController.setCenter(point);
//				        	mMapController.animateTo(point);
//				        	mMapController.setZoom(10);
//				        }
//				        Message msgs = new Message();
//						msgs.what = 0x002;
//						handler.sendMessage(msgs);
//					};
//				}.execute();
	 	       
//			}catch (Exception e) {
//				mMapController.setCenter(point);
//				mMapController.animateTo(point);
//				mMapController.setZoom(10);
//			}
//        }else{
        	mapView.getController().setCenter(point);
    		mapView.getController().animateTo(point);
        	mapView.getController().setZoom(10);
//        }
		mapView.setBuiltInZoomControls(true);
	}

	private Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			super.handleMessage(msg);
			if(msg.what == 0x001){
				try{dialog.show();}catch (Exception e) {
				}
			}else if(msg.what == 0x002){
				try{dialog.cancel();}catch (Exception e) {
				}
			}
		};
	};
	
	@Override
	protected void onDestroy() {
		if (mapManager != null) {
			mapManager.destroy();
			mapManager = null;
		}
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		if (mapManager != null) {
			mapManager.stop();
		}
		super.onPause();
	}

	@Override
	protected void onResume() {
		if (mapManager != null) {
			mapManager.start();
		}
		super.onResume();
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}

}